package com.sohu.peak.gallery.service;

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.lang.StringUtils;

import com.sohu.peak.gallery.domain.Constants;
import com.sohu.peak.util.Logger;

public class CmsNewsThread {
	
	private static class Notify extends Thread {
		private static final Logger log = new Logger();
		private String newsTitle;
		private String newsRetUrl;
		private String content;
		private String parentName;
		private String priority;
		private String cmsUserId;
		private String srcsys;
		
		
//		public Notify(String newsTitle,String newsRetUrl,String content,String parentName,String priority, String cmsUserId){
//			this.newsTitle = newsTitle;
//			this.newsRetUrl = newsRetUrl;
//			this.content = content;
//			this.parentName = parentName;
//			this.priority=priority;
//			this.cmsUserId = cmsUserId;
//		}
		public Notify(String newsTitle,String newsRetUrl,String content,String parentName,String priority, String cmsUserId, String srcsys){
			this.newsTitle = newsTitle;
			this.newsRetUrl = newsRetUrl;
			this.content = content;
			this.parentName = parentName;
			this.priority=priority;
			this.cmsUserId = cmsUserId;
			this.srcsys = srcsys;
		}
		
		public void run(){
			if(addCmsNews())
				log.info("Add cms news success.");
			else
				log.info("Add cms news fail.");
		}

		private boolean addCmsNews()
		{
			if(newsTitle==null||newsRetUrl==null||content==null||parentName==null||cmsUserId==null||priority==null){
				return false;
			}
			String addNewsUrl = Constants.cmsAddNews;
			if(StringUtils.isNotEmpty(this.srcsys) && "cms5".equalsIgnoreCase(srcsys)){
				addNewsUrl = Constants.cms5AddNews;
			}
			
			log.debug("%s?newsTitle=%s&newsRetUrl=%s&content=%s&pName=%s&priority=%s&userId=%s"
					,addNewsUrl,newsTitle,newsRetUrl,content,parentName,priority,cmsUserId);
			HttpClient client = new HttpClient();
			PostMethod method = null;
			method = new GBKPostMethod(addNewsUrl);
			NameValuePair a = new NameValuePair("newsTitle", newsTitle);
	        NameValuePair b = new NameValuePair("newsRetUrl", newsRetUrl);
	        NameValuePair c = new NameValuePair("content", content);
	        NameValuePair d = new NameValuePair("pName", parentName);
	        NameValuePair e = new NameValuePair("priority", priority);
	        NameValuePair f = new NameValuePair("userId", cmsUserId);
	        NameValuePair[] all = new NameValuePair[] {a,b,c,d,e,f};
	        method.setRequestBody(all);
			int statusCode;
			try {
				statusCode = client.executeMethod(method);
				String returnInfo = method.getResponseBodyAsString();
				returnInfo=returnInfo!=null?returnInfo.trim():"";
				if(statusCode!=HttpStatus.SC_OK || returnInfo.contains("error")){
					log.error("statusCode:%s,response:%s",statusCode,returnInfo);
					return false;
	        	}else{
	        		log.debug("statusCode:%s,response:%s",statusCode,returnInfo);
	        	}
			} catch (HttpException he) {
				log.error("client.executeMethod HttpException:", he.getMessage());
				return false;
			} catch (IOException ioe) {
				log.error("client.executeMethod IOException:", ioe.getMessage());
				return false;
			}finally{
				method.releaseConnection();
	        }
			return true;		
		}
	}

//	public static Thread getThread(String newsTitle,String newsRetUrl,String content,String parentName,String priority,String cmsUserId){
//		return new Notify(newsTitle, newsRetUrl, content, parentName, priority, cmsUserId);
//	}
	
	public static Thread getThread(String newsTitle,String newsRetUrl,String content,String parentName,String priority,String cmsUserId, String srcsys){
		return new Notify(newsTitle, newsRetUrl, content, parentName, priority, cmsUserId, srcsys);
	}
	
	public static class GB2312PostMethod extends PostMethod{
		public GB2312PostMethod(String url){
			super(url);
		}
		@Override
		public String getRequestCharSet() {
			return "gb2312";
		}
	}
	
	public static class GBKPostMethod extends PostMethod{
		public GBKPostMethod(String url){
			super(url);
		}
		@Override
		public String getRequestCharSet() {
			return "gbk";
		}
	}
}
